'use strict';
var _ = require('lodash');
var engines = module.exports;

// Underscore
// ----------

// Underscore templates facade.
//
// Special kind of markers `<%%` for opening tags can be used to escape the
// placeholder opening tag. This is often useful for templates including
// snippet of templates you don't want to be interpolated.

/**
 * Constructor overwrite
 */
engines.underscore = function(source, data, options) {
    source = source.replace(engines.underscore.options.matcher, function (m, content) {
    // let's add some funny markers to replace back when templating is done,
    // should be fancy enough to reduce frictions with files using markers like
    // this already.
    return '(;>%%<;)' + content + '(;>%<;)';
    });

    //let the user an option to use settings of _.template
    source = _.template(source, null, options)(data);

    source = source
    .replace(/\(;>%%<;\)/g, engines.underscore.options.start)
    .replace(/\(;>%<;\)/g, engines.underscore.options.end);

    return source;
};

engines.underscore.options = {
    matcher: /<%%([^%]+)%>/g,
    detecter: /<%%?[^%]+%>/,
    start: '<%',
    end: '%>'
};
/**
 * detect the body
 */
engines.underscore.detect = function(body) {
    return engines.underscore.options.detecter.test(body);
};
